package com.zhs.gccs.view.gt;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;


import com.zhs.gccs.R;
import com.zhs.gccs.model.CheckBoxModel;

import java.util.List;

/**
 * Created by 任伟伟
 * Datetime: 2016/11/18-11:07
 * Email: renweiwei@ufashion.com
 */

public class GbItemAdapter extends RecyclerView.Adapter<GbItemAdapter.ViewHolder> {
    private final Context mContext;
    private final List<CheckBoxModel> datas;


    public GbItemAdapter(Context context, List<CheckBoxModel> datas) {
        this.datas = datas;
        this.mContext = context;
    }

    //创建新View，被LayoutManager所调用
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_item_year, viewGroup, false);
        ViewHolder vh = new ViewHolder(view);
        return vh;
    }

    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, @SuppressLint("RecyclerView") final int position) {
        CheckBoxModel gtLb = datas.get(position);
        viewHolder.tv_title.setText(!TextUtils.isEmpty(gtLb.getName())?gtLb.getName():gtLb.getTitle());
        viewHolder.cb_selected.setChecked(gtLb.isSelected());
        viewHolder.ll_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(operationListener!=null)operationListener.onItemClick(position);
            }
        });
    }

    //获取数据的数量
    @Override
    public int getItemCount() {
//        if(datas!=null&&datas.size()>3)return 3;
        return datas != null ? datas.size() : 0;
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public View ll_item;
        public TextView tv_title;
        public CheckBox cb_selected;

        public ViewHolder(View view) {
            super(view);
            ll_item = view.findViewById(R.id.ll_item);
            tv_title = view.findViewById(R.id.tv_title);
            cb_selected = view.findViewById(R.id.cb_selected);
        }
    }

    public interface OperationListener {
        void onItemClick(int position);
    }

    private OperationListener operationListener;

    public OperationListener getOperationListener() {
        return operationListener;
    }

    public void setOperationListener(OperationListener operationListener) {
        this.operationListener = operationListener;
    }
}
